import re, sys
sys.path.append('..')
from urllib2 import urlopen
from flag_quiz import cia_flag_url

def get_cia_data():
    ignore_list = 'at ax bq bv ck cr dq dx fq fs hm hq ip jn jq kq lq mq nc sq uk um us wq'.split(' ')
    try:
        lines = urlopen(cia_flag_url).readlines()
    except:
        lines = []
    img_re = re.compile(r'<img src="([^"]+-lgflag.gif)"[^>]* alt="Flag of ([^"]+)"')
    code_re = re.compile(r'([a-z]{2})-lgflag.gif')
    comma_re = re.compile(r'(.+), (.+)')
    for line in lines:
        found = img_re.search(line)
        if not found: continue
        img_url = found.group(1)
        code = code_re.findall(img_url)[0]
        if code in ignore_list: continue
        name = found.group(2)
        if name.count(', ') == 1: name = ' '.join(reversed(name.split(', ')))
        print '"%s","%s","%s"' %(code, name, img_url)

if __name__ == '__main__':
    get_cia_data()
